package com.ccepc.algmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccepc.algmanage.mapper.AlgorithmConfigMapper;
import com.ccepc.algmanage.service.AlgorithmConfigService;
import com.ccepc.algmanage.service.DeployInfoService;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.demo.api.dto.AlgorithmConfigDto;
import com.ccepc.demo.api.entity.AlgorithmConfig;
import com.ccepc.demo.api.entity.DeployInfo;
import com.ccepc.demo.api.vo.AlgorithmConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-15 09:12:19
 */
@Slf4j
@Service
public class AlgorithmConfigServiceImpl extends ServiceImpl<AlgorithmConfigMapper, AlgorithmConfig> implements AlgorithmConfigService {
    @Resource
    private DeployInfoService deployInfoService;

    @Transactional
    @Override
    public Object saveOrUpdateDto(AlgorithmConfigDto algorithmConfigDto) {
        DeployInfo deployInfo = algorithmConfigDto.getDeployInfo();
        AlgorithmConfig algorithmConfig = algorithmConfigDto.getAlgorithmConfig();
        CommonUtils.saveOrUpdate(deployInfo,deployInfoService);
        if (deployInfo.getPushmanId() == null) {
            deployInfoService.update(new LambdaUpdateWrapper<DeployInfo>()
                    .eq(DeployInfo::getId, deployInfo.getId())
                    .set(DeployInfo::getPushmanId, null));
        }
        algorithmConfig.setDeployId(deployInfo.getId());
        if (CommonUtils.saveOrUpdate(algorithmConfig,this) == 1) {
            if (algorithmConfig.getCameraIds() == null) {
                this.update(new LambdaUpdateWrapper<AlgorithmConfig>()
                        .eq(AlgorithmConfig::getId, algorithmConfig.getId())
                        .set(AlgorithmConfig::getCameraIds, null));
            }
        }
        return algorithmConfig.getId();
    }

    @Override
    public Object getByDeployId(Integer deployId) {
        AlgorithmConfigVo algorithmConfigVo = new AlgorithmConfigVo();
        AlgorithmConfig algorithmConfig = this.getOne(
                new LambdaQueryWrapper<AlgorithmConfig>()
                        .eq(AlgorithmConfig::getDeployId, deployId)
        );
        algorithmConfigVo.setAlgorithmConfig(algorithmConfig);
        DeployInfo deployInfo = deployInfoService.getById(deployId);
        algorithmConfigVo.setPushmanId(deployInfo.getPushmanId());
        algorithmConfigVo.setPushruleId(deployInfo.getPushruleId());
        algorithmConfigVo.setServerId(deployInfo.getServerId());
        return algorithmConfigVo;
    }

}
